﻿using UnityEngine;
using System.Collections;
using Assets.MatchVS;
using System;
using System.Collections.Generic;
using LitJson;

public class TopList : MonoBehaviour {
    public UIGrid listView;
    private int count;
    class Rank : IComparer<Rank> {
        public String headimgurl { get; set; }
        public String nickname { get; set; }
        public String userid { get; set; }
        public int rank { get; set; }
        public int value { get; set; }

        int IComparer<Rank>.Compare(Rank x, Rank y) {
            return x.rank-y.rank;
        }
    }

    void Start() {
//{ "data":{ "gameID":"102000","item":"extend_1","nums":20,"result":[
        //{"headimgurl":"http://pic.vszone.cn/upload/head/1416997282747.jpg","nickname":"旷华月","rank":1,"userid":"39784","value":2},{"headimgurl":"http://pic.vszone.cn/upload/head/1416997090890.jpg","nickname":"登卓君","rank":2,"userid":"36785","value":2},{"headimgurl":"http://pic.vszone.cn/upload/head/1416997025346.jpg","nickname":"南区第一帅","rank":3,"userid":"23","value":0},{"headimgurl":"http://pic.vszone.cn/upload/head/1416997282747.jpg","nickname":"南区第一帅","rank":4,"userid":"32","value":0},{"headimgurl":"http://pic.vszone.cn/upload/head/1416997090890.jpg","nickname":"完琳晨","rank":5,"userid":"39993","value":0},{"headimgurl":"http://pic.vszone.cn/upload/head/1416997282747.jpg","nickname":"可爱﹏姚歆","rank":6,"userid":"41018","value":0},{"headimgurl":"http://pic.vszone.cn/upload/head/1416997342935.jpg","nickname":"可爱№皇甫小喵","rank":7,"userid":"41020","value":0},{"headimgurl":"http://pic.vszone.cn/upload/head/1416997134960.jpg","nickname":"可爱℡ 童皓轩","rank":8,"userid":"41022","value":0},{"headimgurl":"http://pic.vszone.cn/upload/head/1416997090890.jpg","nickname":"哭泣⌒西门博","rank":9,"userid":"41024","value":0},{"headimgurl":"http://pic.vszone.cn/upload/head/1416997025346.jpg","nickname":"哭泣〝程俊驰","rank":10,"userid":"41026","value":0},{"headimgurl":"http://pic.vszone.cn/upload/head/1416997282747.jpg","nickname":"哭泣√莫伟诚","rank":11,"userid":"41031","value":0},{"headimgurl":"http://pic.vszone.cn/upload/head/1416997134960.jpg","nickname":"可爱$龙风华","rank":12,"userid":"41032","value":0},{"headimgurl":"http://pic.vszone.cn/upload/head/1416997282747.jpg","nickname":"快乐$敖曜","rank":13,"userid":"41034","value":0},{"headimgurl":"http://pic.vszone.cn/upload/head/1416997330299.jpg","nickname":"哭泣★索菁","rank":14,"userid":"41050","value":0},{"headimgurl":"http://pic.vszone.cn/upload/head/1416997090890.jpg","nickname":"快乐★管馨彤","rank":15,"userid":"41052","value":0},{"headimgurl":"http://pic.vszone.cn/upload/head/1416997330299.jpg","nickname":"伤心§华月曜","rank":16,"userid":"41060","value":0},{"headimgurl":"http://pic.vszone.cn/upload/head/1416997025346.jpg","nickname":"可爱☞商玥怡","rank":17,"userid":"41062","value":0},{"headimgurl":"http://pic.vszone.cn/upload/head/1416997342935.jpg","nickname":"快乐∈吉娅楠","rank":18,"userid":"41577","value":0},{"headimgurl":"http://pic.vszone.cn/upload/head/1416997025346.jpg","nickname":"可爱〝沙明溪","rank":19,"userid":"41578","value":0},{"headimgurl":"http://pic.vszone.cn/upload/head/1416997025346.jpg","nickname":"快乐~辛香凝","rank":20,"userid":"41579","value":0}],"ruleA":"desc","type":1},"status":0}	解决方案'MatchVSDemo-Unity3D' ‎(3 个项目)	Assets/MatchVS/Http/Http.cs	148	

        UIUtil.setListener("BtnRefresh", onClick);
        Http.getItemRank(this, (text, err) => {
            JsonData pa = LitJson.JsonMapper.ToObject(text);
            pa = pa["data"]["result"];
            Debug.Log(pa.ToJson());
            List<Rank> ranks = LitJson.JSON<List<Rank>>.toJsonObject(pa.ToJson());
            ranks.Sort(new Rank());
            //ranks.Reverse();
            UIUtil.removeAll(listView);
            int i =0;
            foreach(var item in ranks) {
                Debug.Log(item.nickname+" rank:"+ item.rank);
                AddItem(item.nickname, item.value+"", item.userid,item.headimgurl, i);
                i++;
            }
          
        }, MatchVS.gameID, MatchVS.item,10);
    }



    private void AddItem(String nick,String score,String uid,String url,int index) {
        GameObject o = (GameObject)Instantiate(Resources.Load("ItemListView"));
        o.name = "item" + index;
        //GameObject root = GameObject.Find("GameObject");       
        GameObject avator = o.transform.Find("Avatar").gameObject;
        avator.name = "avator" + index;
        o.transform.Find("Name").GetComponent<UILabel>().text= " 第" + index + "名 "+nick ;
        o.transform.Find("Score").GetComponent<UILabel>().text = "天梯积分:"+score;
        UIUtil.setListener("avator" + index, onClick);
        //Debug.Log(nick);
        listView.AddChild(o.transform);
        GameObject item = GameObject.Find(o.name);
        item.transform.localPosition = new Vector3(0, 0, 0);
        item.transform.localScale = new Vector3(1, 1, 1);
        listView.Reposition();
        GameObject texture = o.transform.Find("Texture").gameObject;
        Debug.Log("texture:" + texture.transform);
        UITexture headerImg = texture.GetComponent<UITexture>();
        ImageLoader.getInstance().load(headerImg, url);


    }

    void onClick(GameObject button) {
        Debug.Log("GameObject " + button.name);
        switch(button.name) {
        case "BtnRefresh":
            UIUtil.removeAll(listView);
            Start();
        break;

        default:
                break;
        }

    }
}
